package com.sitech.timing;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

import com.sitech.timing.domain.GroupTaskInfo;
import com.sitech.timing.domain.GroupTaskInstance;
import com.sitech.timing.domain.ParamInfo;
import com.sitech.timing.domain.TaskInfo;
import com.sitech.timing.domain.TaskInstance;

 class GroupTaskContext
{
	private static Map<String,GroupTaskInfo> groupTaskDefineMap = new ConcurrentHashMap<String, GroupTaskInfo>();
	private static Map<String,GroupTaskInstance> groupTaskRunMap = new ConcurrentHashMap<String, GroupTaskInstance>();
	private static Map<String,ThreadPoolExecutor> threadPoolExecutorMap = new ConcurrentHashMap<String,ThreadPoolExecutor>();
	private static Map<String,Map<String,TaskInstance>> taskRunMap = new ConcurrentHashMap<String, Map<String,TaskInstance>>();
	private static Map<String,ParamInfo> paramInfoMap = new ConcurrentHashMap<String, ParamInfo>();


	public synchronized static void addRunTaskGroup(GroupTaskInstance groupTaskInstance)
	{
		synchronized (groupTaskRunMap)
		{
			groupTaskRunMap.put(groupTaskInstance.getGroupTaskInstanceId(), groupTaskInstance);
		}
	}
	
	public static Map<String,GroupTaskInstance> getAllRunGroupTask()
	{
		synchronized (groupTaskRunMap)
		{
			Map<String, GroupTaskInstance> map = new HashMap<String, GroupTaskInstance>();
			for(Map.Entry<String ,GroupTaskInstance> entry : groupTaskRunMap.entrySet())
			{
				map.put(entry.getKey(), entry.getValue().clone());
			}
			return map;
		}

	}
	
	public static GroupTaskInstance getRunTaskGroup(String groupTaskInstanceId)
	{
		synchronized (groupTaskRunMap)
		{
			if(groupTaskRunMap != null && groupTaskRunMap.containsKey(groupTaskInstanceId))
			{
				return groupTaskRunMap.get(groupTaskInstanceId);
			}
			else
			{
				return null;
			}
		}
	}
	
	public static TaskInstance getRunTask(String groupTaskInstanceId, String taskInstanceId)
	{
		synchronized (taskRunMap)
		{
			if(taskRunMap.containsKey(groupTaskInstanceId))
			{
				Map<String,TaskInstance> map = taskRunMap.get(groupTaskInstanceId);
				if(map.containsKey(taskInstanceId))
				{
					return map.get(taskInstanceId);
				}
			}
			return null;
		}
	}
	
	public static Map<String,TaskInstance> getALLRunTask(String groupTaskInstanceId)
	{
		synchronized (taskRunMap)
		{
			if(taskRunMap.containsKey(groupTaskInstanceId))
			{
				return taskRunMap.get(groupTaskInstanceId);
			}
			else
			{
				return null;
			}
		}
	}

	
	public synchronized static void addRunTask(TaskInstance taskInstance)
	{
		synchronized (groupTaskRunMap)
		{
			if (groupTaskRunMap.containsKey(taskInstance.getGroupTaskInstanceId()))
			{
				GroupTaskInstance groupTaskInstance = groupTaskRunMap.get(taskInstance.getGroupTaskInstanceId());
				if (groupTaskInstance.getRunTaskInstanceMap() == null)
				{
					groupTaskInstance.setRunTaskInstanceMap(new HashMap<String, TaskInstance>());
				}
				groupTaskInstance.getRunTaskInstanceMap().put(taskInstance.getTaskInstanceId(), taskInstance);
			}
		}
		
		synchronized (taskRunMap)
		{
			if (taskRunMap.containsKey(taskInstance.getGroupTaskInstanceId()))
			{
				if (taskRunMap.get(taskInstance.getGroupTaskInstanceId()) != null)
				{
					taskRunMap.get(taskInstance.getGroupTaskInstanceId()).put(taskInstance.getTaskInstanceId(),
							taskInstance);
				}
				else
				{
					Map<String, TaskInstance> map = new ConcurrentHashMap<String, TaskInstance>();
					map.put(taskInstance.getTaskInstanceId(), taskInstance);
					taskRunMap.put(taskInstance.getGroupTaskInstanceId(), map);
				}
			}
			else
			{
				Map<String, TaskInstance> map = new ConcurrentHashMap<String, TaskInstance>();
				map.put(taskInstance.getTaskInstanceId(), taskInstance);
				taskRunMap.put(taskInstance.getGroupTaskInstanceId(), map);
			}
		}
	}
	
	public synchronized static void taskEnd(TaskInstance taskInstance)
	{
		synchronized (groupTaskRunMap)
		{
			if(groupTaskRunMap.containsKey(taskInstance.getGroupTaskInstanceId()))
			{
				GroupTaskInstance groupTaskInstance = groupTaskRunMap.get(taskInstance.getGroupTaskInstanceId());
				if(groupTaskInstance.getRunTaskInstanceMap() != null)
				{
					groupTaskInstance.getRunTaskInstanceMap().remove(taskInstance.getTaskInstanceId());
				}
			}
		}

		synchronized (taskRunMap)
		{
			if(taskRunMap.containsKey(taskInstance.getGroupTaskInstanceId()))
			{
				Map<String,TaskInstance> map = taskRunMap.get(taskInstance.getGroupTaskInstanceId());
				if(map.containsKey(taskInstance.getTaskInstanceId()))
				{
					map.remove(taskInstance.getTaskInstanceId());
				}
			}
		}

	}
	
	public static void groupTaskEnd(String groupTaskInstanceId)
	{
		synchronized (groupTaskRunMap)
		{
			if(groupTaskRunMap.containsKey(groupTaskInstanceId))
			{
				groupTaskRunMap.remove(groupTaskInstanceId);
			}
		}
	}
	
	
	public static void addGroupTaskInfo(GroupTaskInfo groupTaskInfo)
	{
		synchronized (groupTaskDefineMap)
		{
			groupTaskDefineMap.put(groupTaskInfo.getGroupTaskId(), groupTaskInfo);
		}
	}
	
	public static void removeGroupTaskInfo(String groupTaskId)
	{
		synchronized (groupTaskDefineMap)
		{
			groupTaskDefineMap.remove(groupTaskId);
		}
	}
	
	public static GroupTaskInfo getGroupTaskInfo(String groupTaskId)
	{
		synchronized (groupTaskDefineMap)
		{
			return groupTaskDefineMap.get(groupTaskId);
		}
	}
	
	public static TaskInfo getTaskInfo(String groupTaskId,String taskInfoId)
	{
		synchronized (groupTaskDefineMap)
		{
			GroupTaskInfo groupTaskInfo  =  groupTaskDefineMap.get(groupTaskId);
			List<TaskInfo> taskInfoList = groupTaskInfo.getTaskInfoList();
			for(TaskInfo taskInfo : taskInfoList)
			{
				if(taskInfoId.equals(taskInfo.getTaskInfoId()))
				{
					return taskInfo;
				}
			}
			return null;
		}
	}
	
	/**
	 * task 运行错误，则整个taskgroup 以及其他兄弟task均取消执行
	 * */
	public static void taskRunError(TaskInstance taskInstance)
	{
		synchronized (groupTaskRunMap)
		{
			groupTaskRunMap.remove(taskInstance.getGroupTaskInstanceId());
		}

		synchronized (taskRunMap)
		{
			taskRunMap.remove(taskInstance.getGroupTaskInstanceId());
		}

	}
	
	public static ThreadPoolExecutor getThreadPoolExecutor(String taskTypeId)
	{
		return threadPoolExecutorMap.get(taskTypeId);
	}
	
	public static void addThreadPoolExecutor(String taskTypeId, ThreadPoolExecutor threadPoolExecutor)
	{
		synchronized (threadPoolExecutorMap)
		{
			threadPoolExecutorMap.put(taskTypeId, threadPoolExecutor);
		}
	}
	
	public static void addParamInfo(ParamInfo paramInfo)
	{
		synchronized (paramInfoMap)
		{
			paramInfoMap.put(paramInfo.getParamInfoId(), paramInfo);
		}
	}
	
	public static ParamInfo getParamInfo(String paramInfoId)
	{
		synchronized (paramInfoMap)
		{
			return  paramInfoMap.get(paramInfoId);
		}
	}
	
	public synchronized static void refeshALL()
	{
		groupTaskDefineMap = new ConcurrentHashMap<String, GroupTaskInfo>();
		groupTaskRunMap = new ConcurrentHashMap<String, GroupTaskInstance>();
		threadPoolExecutorMap = new ConcurrentHashMap<String,ThreadPoolExecutor>();
		taskRunMap = new ConcurrentHashMap<String, Map<String,TaskInstance>>();
		paramInfoMap = new ConcurrentHashMap<String, ParamInfo>();
	}
}
